using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;

namespace TXLooker.Common
{
    public static class DllImportCommon
    {
        // Fields
        public const int ERROR_BUFFER_OVERFLOW = 0x6f;
        public const int MAX_ADAPTER_ADDRESS_LENGTH = 8;
        public const int MAX_ADAPTER_DESCRIPTION_LENGTH = 0x80;
        public const int MAX_ADAPTER_NAME_LENGTH = 0x100;
        public const int MIB_IF_TYPE_ETHERNET = 6;
        public const int MIB_IF_TYPE_FDDI = 15;
        public const int MIB_IF_TYPE_LOOPBACK = 0x18;
        public const int MIB_IF_TYPE_OTHER = 1;
        public const int MIB_IF_TYPE_PPP = 0x17;
        public const int MIB_IF_TYPE_SLIP = 0x1c;
        public const int MIB_IF_TYPE_TOKENRING = 9;

        // Methods
        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("iphlpapi.dll", CharSet = CharSet.Ansi)]
        public static extern int GetAdaptersInfo(IntPtr pAdapterInfo, ref long pBufOutLen);
        [DllImport("user32.dll")]
        public static extern IntPtr GetForegroundWindow();
        [DllImport("kernel32.dll")]
        public static extern IntPtr OpenProcess(ProcessAccessFlags dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, uint dwProcessId);
        [return: MarshalAs(UnmanagedType.Bool)]
        [DllImport("user32.dll")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);
        [return: MarshalAs(UnmanagedType.Bool)]
        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern bool TerminateProcess(IntPtr hProcess, uint uExitCode);

        [DllImport("ole32.dll")]
        public static extern int CoMarshalInterThreadInterfaceInStream([In] ref Guid riid, [MarshalAs(UnmanagedType.IUnknown)] object pUnk, out IStream ppStm);

        [DllImport("ole32.dll")]
        public static extern int CoGetInterfaceAndReleaseStream(IStream pStm, [In] ref Guid riid, [MarshalAs(UnmanagedType.IUnknown)] out object ppv);

        // Nested Types
        [Flags]
        public enum InternetGetConnectedStateFlags
        {
            INTERNET_CONNECTION_CONFIGURED = 0x40,
            INTERNET_CONNECTION_LAN = 2,
            INTERNET_CONNECTION_MODEM = 1,
            INTERNET_CONNECTION_NOTCCONFIGURED = 0x12,
            INTERNET_CONNECTION_OFFLINE = 0x20,
            INTERNET_CONNECTION_PROXY = 4,
            INTERNET_CONNECTION_RAS_INSTALLED = 0x10
        }

        [StructLayout(LayoutKind.Sequential, Pack = 1)]
        public struct IP_ADAPTER_INFO
        {
            public IntPtr Next;
            public int ComboIndex;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
            public string AdapterName;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x84)]
            public string AdapterDescription;
            public uint AddressLength;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
            public byte[] Address;
            public int Index;
            public uint Type;
            public uint DhcpEnabled;
            public IntPtr CurrentIpAddress;
            public DllImportCommon.IP_ADDR_STRING IpAddressList;
            public DllImportCommon.IP_ADDR_STRING GatewayList;
            public DllImportCommon.IP_ADDR_STRING DhcpServer;
            public bool HaveWins;
            public DllImportCommon.IP_ADDR_STRING PrimaryWinsServer;
            public DllImportCommon.IP_ADDR_STRING SecondaryWinsServer;
            public int LeaseObtained;
            public int LeaseExpires;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct IP_ADDR_STRING
        {
            public IntPtr Next;
            public DllImportCommon.IP_ADDRESS_STRING IpAddress;
            public DllImportCommon.IP_ADDRESS_STRING IpMask;
            public int Context;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct IP_ADDRESS_STRING
        {
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x10)]
            public string Address;
        }

        [Flags]
        public enum ProcessAccessFlags
        {
            PROCESS_CREATE_PROCESS = 0x80,
            PROCESS_CREATE_THREAD = 2,
            PROCESS_DUP_HANDLE = 0x40,
            PROCESS_QUERY_INFORMATION = 0x400,
            PROCESS_SET_INFORMATION = 0x200,
            PROCESS_SET_QUOTA = 0x100,
            PROCESS_SET_SESSIONID = 4,
            PROCESS_SUSPEND_RESUME = 0x800,
            PROCESS_TERMINATE = 1,
            PROCESS_VM_OPERATION = 8,
            PROCESS_VM_READ = 0x10,
            PROCESS_VM_WRITE = 0x20
        }
    }
}